package com.ihd.ihardwarefactorytools.view.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableArrayList;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.ihd.ihardwarefactorytools.databinding.ItemListBinding;
import com.ihd.ihardwarefactorytools.databinding.ItemPopupKeyValueBinding;
import com.ihd.ihardwarefactorytools.R;
import com.ihd.ihardwarefactorytools.pojo.ListRes;


import java.util.List;

import cn.wowjoy.commonlibrary.LiveDataBus.LiveDataBus;
import cn.wowjoy.commonlibrary.adapter.CommonAdapter;
import cn.wowjoy.commonlibrary.utils.DensityUtil;


public class MainViewModel extends AndroidViewModel {

    private Application mApplication;

    public MainViewModel(@NonNull Application application) {
        super(application);
        mApplication = application;
    }

    private ObservableArrayList<String> mWhoType = new ObservableArrayList<>();
    public CommonAdapter mWhoAdapter = new CommonAdapter<String, ItemPopupKeyValueBinding>(R.layout.item_popup_key_value, mWhoType, new CommonAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
            LiveDataBus.get().with("1").setValue(position);
        }
    }) {
    };

    public void setActions() {
        mWhoType.add("清空");
    }

    private ObservableArrayList<ListRes> mConcerned = new ObservableArrayList<>();
    public CommonAdapter mConcernedAdapter = new CommonAdapter<ListRes, ItemListBinding>(R.layout.item_list, mConcerned, null) {
        @Override
        public void convert(ItemListBinding mDataBinding, ListRes test_concern, int position) {
            GridLayoutManager.LayoutParams vl = (GridLayoutManager.LayoutParams) mDataBinding.itemLL.getLayoutParams();
            if (position % 2 == 0) {
                vl.leftMargin = 0;
            } else {
                vl.leftMargin = -DensityUtil.dip2px(1);
            }
            mDataBinding.itemLL.setLayoutParams(vl);
        }
    };

    public void setList(List<ListRes> rd) {
        mConcerned.clear();
        if (rd != null && rd.size() > 0)
            mConcerned.addAll(rd);
    }
}
